package com.zr.spring.proxy.jdk;

import com.zr.spring.utils.GenerateClassFile;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyHandler implements InvocationHandler {

    private Object target;

    private Object proxy;

    public MyHandler(Object target) {
        this.target = target;
    }

    /**
     * 获取代理类
     *
     * @param <T>
     * @return
     */
    public <T> T getProxy() {
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("请选择动物要吃的食物");

        method.invoke(target, args);

        System.out.println("已经选择了要吃的食物");
        return null;
    }


}
